import fetchJsonp from 'fetch-jsonp'
import { TIMEOUT } from './http'

export type JsonpOptions = fetchJsonp.Options

/**
 * @description 发送 jsonp 请求
 * */
const sendJsonp = <T>(url: string, options?: JsonpOptions) => {
  options = options || {}
  options.timeout = options.timeout || TIMEOUT
  return new Promise<T>((resolve, reject) => {
    fetchJsonp(url, options)
      .then((res) => resolve(res.json() as T))
      .catch((err) => {
        console.log('jsonp 请求失败 >>>', err)
        // TODO 监控
        reject(err)
      })
  })
}

export default sendJsonp
